package com.xiaoyao.novel.action.page;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.xiaoyao.novel.common.Constant;

/**
 * 封装的 Action 基类.
 * 
 * @author 旷明爱
 * @date Dec 13, 2012 3:35:05 PM
 */
public class BaseAction extends ActionSupport {
	protected static Logger logger = Logger.getLogger(BaseAction.class);
	private static final long serialVersionUID = 1L;

	protected HttpServletRequest request = getRequest();
	protected HttpServletResponse response = getResponse();
	protected HttpSession session = getSession();

	protected HttpServletRequest getRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		// try {
		// request.setCharacterEncoding(Constant.CHARSET);
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// }
		return request;
	}

	/**
	 * struts自动设置的参数才会编码.
	 * 
	 * @param key
	 * @return
	 */
	protected String getParameter(String key) {
		String value = request.getParameter(key);
		try {
			value = new String(value.getBytes("ISO8859-1"), Constant.CHARSET);
		}catch (Exception e) {
			logger.error(e);
		}
		return value;
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected HttpSession getSession() {
		return request.getSession();
	}

	protected String toJson(Object object) {
		return object.toString();
	}

	protected void output(String data) {
		HttpServletResponse response = getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			logger.error("sendData error", e);
		}
		writer.write(data);
		writer.flush();
		// writer.close();
	}

	/**
	 * 输出JSON 格式.
	 * 
	 * @param data
	 */
	protected void outputJson(String data) {
		HttpServletResponse response = getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			logger.error("sendData error", e);
		}
		writer.write(data);
		writer.flush();
		writer.close();
	}

	/**
	 * 格式化参数列表
	 * 
	 * @param paramMap
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected String formatParam() {
		Enumeration<String> paramEnum = request.getParameterNames();
		StringBuffer buffer = new StringBuffer();
		while (paramEnum.hasMoreElements()) {
			String key = paramEnum.nextElement();
			if (key.equals("parentId")) {// 删掉平台传的参数.
				continue;
			}
			String value = request.getParameter(key);
			buffer.append("&").append(key).append("=").append(value);
		}
		return buffer.toString();
	}

	protected String getIp() {
		String ip = "";
		if (request.getHeader("x-forwarded-for") == null) {
			ip = request.getRemoteAddr();
		} else {
			ip = request.getHeader("x-forwarded-for");
		}
		return ip;
	}

}
